package com.rdc.qingkou.adapter;


import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.ui.CommentActivity;
import com.rdc.qingkou.ui.OtherHomepageActivity;
import com.rdc.qingkou.ui.PersonalHomepageActivity;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.view.fragment.OneLevelCommentFragment;
import com.rdc.qingkou.view.fragment.TwoLevelCommentFragment;
import com.rdc.qingkou.vo.CommentOneLevelVO;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class OneLevelCommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int TYPE_HOT_COMMENT = 0;
    private final static int TYPE_RECENT_COMMENT = 1;
    private final static int TYPE_TITLE_HOT = 2;
    private final static int TYPE_TITLE_RECENT = 3;
    private final static int TYPE_NONE = 4;

    private List<CommentOneLevelVO> mHotCommentList;
    private List<CommentOneLevelVO> mRecentCommentList;
    private int mHotCommentNum;
    private int mRecentCommentNum;
    private boolean hasHotComment;
    private boolean hasRecentComment;
    private CommentActivity mCommentActivity;
    private OneLevelCommentFragment mOneLevelCommentFragment;

    public OneLevelCommentRecyclerViewAdapter(CommentActivity activity,
                                              OneLevelCommentFragment fragment,
                                              List<CommentOneLevelVO> HotCommentList,
                                              List<CommentOneLevelVO> RecentCommentList) {
        mOneLevelCommentFragment = fragment;
        mHotCommentList = HotCommentList;
        mRecentCommentList = RecentCommentList;
        mCommentActivity = activity;
        hasHotComment = mHotCommentList.size() != 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_HOT_COMMENT) {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_recyclerview, parent, false);
            HotCommentViewHolder holder = new HotCommentViewHolder(view);
            setLongClickListener(view, holder);
            return holder;
        } else if (viewType == TYPE_RECENT_COMMENT) {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_recyclerview, parent, false);
            RecentCommentViewHolder holder = new RecentCommentViewHolder(view);
            setLongClickListener(view, holder);
            return holder;
        } else if (viewType == TYPE_TITLE_HOT) {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_header, parent, false);
            return new HotHeaderViewHolder(view);
        } else if (viewType == TYPE_TITLE_RECENT) {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_header, parent, false);
            return new RecentHeaderViewHolder(view);
        } else {
            view = LayoutInflater.from(mCommentActivity).inflate(R.layout.item_comment_none, parent, false);
            return new NoneViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int location;
        if (hasHotComment) {
            if (holder instanceof HotCommentViewHolder) {
                location = position - 1;
                setCommentData((BaseCommentViewHolder) holder, mHotCommentList.get(location));
                setClickListener(location, (BaseCommentViewHolder) holder, false);
                return;
            } else if (holder instanceof RecentCommentViewHolder) {
                location = position - 2 - mHotCommentNum;
                setCommentData((BaseCommentViewHolder) holder, mRecentCommentList.get(location));
                setClickListener(location, (BaseCommentViewHolder) holder, true);
                return;
            }
        } else {
            if (holder instanceof RecentCommentViewHolder) {
                location = position - 1;
                setCommentData((BaseCommentViewHolder) holder, mRecentCommentList.get(location));
                setClickListener(location, (BaseCommentViewHolder) holder, true);
                return;
            }
        }
        if (holder instanceof HotHeaderViewHolder) {
            ((HotHeaderViewHolder) holder).hotHeader.setText("热门评论");
        } else if (holder instanceof RecentHeaderViewHolder) {
            ((RecentHeaderViewHolder) holder).recentHeaderComment.setText("最新评论");
        }
    }

    @Override
    public int getItemCount() {
        mHotCommentNum = mHotCommentList.size();
        mRecentCommentNum = mRecentCommentList.size();
        if (mHotCommentNum != 0 && mRecentCommentNum != 0) {
            //热门评论和最近评论都存在
            hasHotComment = true;
            hasRecentComment = true;
            return mHotCommentNum + mRecentCommentNum + 2;
        } else if (mHotCommentNum == 0 && mRecentCommentNum != 0) {
            //只有最近评论
            hasHotComment = false;
            hasRecentComment = true;
            return mRecentCommentNum + 1;
        } else {
            //没有评论
            hasHotComment = false;
            hasRecentComment = false;
            return 1;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (!hasRecentComment) {
            return TYPE_NONE;
        } else if (hasHotComment) {
            //热门评论和最近评论都存在
            if (position == 0) {
                return TYPE_TITLE_HOT;
            } else if (position <= mHotCommentNum) {
                return TYPE_HOT_COMMENT;
            } else if (position == (mHotCommentNum + 1)) {
                return TYPE_TITLE_RECENT;
            } else {
                return TYPE_RECENT_COMMENT;
            }
        } else {
            //只有最近评论
            if (position == 0) {
                return TYPE_TITLE_RECENT;
            } else {
                return TYPE_RECENT_COMMENT;
            }
        }
    }

    /**
     * 给itemView加载数据
     */
    private void setCommentData(BaseCommentViewHolder holder, CommentOneLevelVO comment) {
        Glide.with(mCommentActivity).load(comment.getUser().getHeadImageUrl()).into(holder.headImage);
        holder.name.setText(comment.getUser().getNickName());
        holder.content.setText(comment.getContent());
        holder.date.setText(comment.getCommentDate());
        holder.appreciateNum.setText(String.valueOf(comment.getPraiseNumber()));
        holder.commentNum.setText(String.valueOf(comment.getCommentsNumber()));
        if (comment.getPraiseNumber() > 0) {
            holder.appreciate.setImageResource(R.drawable.button_appreciate_pressure);
        } else {
            holder.appreciate.setImageResource(R.drawable.button_appreciate_empty);
        }
    }

    /**
     * 给itemView中的各种view设置监听事件
     * @param location iew在数据源List中对应的位置
     * @param baseCommentViewHolder view所对应的ViewHolder
     */
    private void setClickListener(final int location, final BaseCommentViewHolder baseCommentViewHolder, final boolean isRecentComment) {
        final CommentOneLevelVO comment;
        if (isRecentComment) {
            comment = mRecentCommentList.get(location);
        } else {
            comment = mHotCommentList.get(location);
        }
        baseCommentViewHolder.headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mCommentActivity, OtherHomepageActivity.class);
                intent.putExtra("user", comment.getUser());
                mCommentActivity.startActivity(intent);
            }
        });
        baseCommentViewHolder.appreciate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MyUtils.isNetworkConnected(mCommentActivity)) {
                    mOneLevelCommentFragment.presenter.appreciate(comment.getCommentId());
                } else {
                    Toast.makeText(mCommentActivity, "无网络！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        baseCommentViewHolder.appreciateNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baseCommentViewHolder.appreciate.performClick();
            }
        });
        baseCommentViewHolder.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TwoLevelCommentFragment f = new TwoLevelCommentFragment();
                f.setParentCommentId(comment.getCommentId());
                mCommentActivity.replaceFragment(f);
            }
        });
        baseCommentViewHolder.commentNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baseCommentViewHolder.comment.performClick();
            }
        });
        baseCommentViewHolder.headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (comment.getUser().getUserId() == MyApplication.getUser().getUserId()) {
                    Intent intent = new Intent(mCommentActivity, PersonalHomepageActivity.class);
                    mCommentActivity.startActivity(intent);
                } else {
                    Intent intent = new Intent(mCommentActivity, OtherHomepageActivity.class);
                    intent.putExtra("user", comment.getUser());
                    mCommentActivity.startActivity(intent);
                }
            }
        });
        baseCommentViewHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                baseCommentViewHolder.headImage.performClick();
            }
        });
    }

    /**
     * 给itemView设置长按监听
     */
    private void setLongClickListener(final View view, final BaseCommentViewHolder viewHolder) {
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                PopupMenu popupMenu = new PopupMenu(mCommentActivity, view);
                popupMenu.getMenuInflater().inflate(R.menu.one_level_comment, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.menu_copy:
                                ClipboardManager cm = (ClipboardManager)
                                        mCommentActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData cd = ClipData.newPlainText("Label", viewHolder.content.getText());
                                cm.setPrimaryClip(cd);
                                Toast.makeText(mCommentActivity, "已经复制到剪切板", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.menu_report:
                                break;
                        }
                        return true;
                    }
                });
                popupMenu.show();
                return true;
            }
        });
    }

    private static class BaseCommentViewHolder extends RecyclerView.ViewHolder {

        CircleImageView headImage;
        TextView name;
        TextView content;
        TextView date;
        ImageView comment;
        ImageView appreciate;
        TextView commentNum;
        TextView appreciateNum;

        BaseCommentViewHolder(View itemView) {
            super(itemView);
            headImage = (CircleImageView) itemView.findViewById(R.id.item_comment_head_imageView);
            name = (TextView) itemView.findViewById(R.id.item_comment_name_textView);
            content = (TextView) itemView.findViewById(R.id.item_comment_content_textView);
            date = (TextView) itemView.findViewById(R.id.item_comment_date_textView);
            comment = (ImageView) itemView.findViewById(R.id.item_comment_comment_imageVIew);
            appreciate = (ImageView) itemView.findViewById(R.id.item_comment_appreciate_imageView);
            commentNum = (TextView) itemView.findViewById(R.id.item_comment_commentNum_textView);
            appreciateNum = (TextView) itemView.findViewById(R.id.item_comment_appreciateNum_textView);
        }
    }

    private static class HotCommentViewHolder extends BaseCommentViewHolder {
        HotCommentViewHolder(View itemView) {
            super(itemView);
        }
    }

    private static class RecentCommentViewHolder extends BaseCommentViewHolder {

        RecentCommentViewHolder(View itemView) {
            super(itemView);
        }
    }

    private static class HotHeaderViewHolder extends RecyclerView.ViewHolder {
        TextView hotHeader;

        HotHeaderViewHolder(View itemView) {
            super(itemView);
            hotHeader = (TextView) itemView.findViewById(R.id.layout_comment_header_textView);
        }
    }

    private static class RecentHeaderViewHolder extends RecyclerView.ViewHolder {
        TextView recentHeaderComment;

        RecentHeaderViewHolder(View itemView) {
            super(itemView);
            recentHeaderComment = (TextView) itemView.findViewById(R.id.layout_comment_header_textView);
        }
    }

    private static class NoneViewHolder extends RecyclerView.ViewHolder {
        NoneViewHolder(View itemView) {
            super(itemView);
        }
    }
}
